"""
测试安装和依赖
运行此脚本检查系统是否正确安装
"""
import sys
from importlib import import_module

def test_import(module_name, package_name=None):
    """测试模块导入"""
    try:
        import_module(module_name)
        print(f"✅ {package_name or module_name}")
        return True
    except ImportError as e:
        print(f"❌ {package_name or module_name}: {e}")
        return False

def test_python_version():
    """测试Python版本"""
    version = sys.version_info
    if version.major >= 3 and version.minor >= 8:
        print(f"✅ Python {version.major}.{version.minor}.{version.micro}")
        return True
    else:
        print(f"❌ Python版本过低: {version.major}.{version.minor}")
        print("   要求: Python 3.8+")
        return False

def main():
    print("=" * 60)
    print("检查依赖安装情况")
    print("=" * 60)
    print()
    
    all_ok = True
    
    # Python版本
    print("Python环境:")
    if not test_python_version():
        all_ok = False
    print()
    
    # 核心依赖
    print("核心依赖:")
    dependencies = [
        ("fastapi", "FastAPI"),
        ("uvicorn", "Uvicorn"),
        ("requests", "Requests"),
        ("beautifulsoup4", "BeautifulSoup4"),
        ("sentence_transformers", "Sentence Transformers"),
        ("faiss", "FAISS"),
        ("transformers", "Transformers"),
        ("streamlit", "Streamlit"),
        ("sklearn", "Scikit-learn"),
    ]
    
    for module, name in dependencies:
        if not test_import(module, name):
            all_ok = False
    
    print()
    
    # 可选依赖
    print("可选依赖:")
    optional = [
        ("rouge_score", "ROUGE Score"),
        ("accelerate", "Accelerate"),
    ]
    
    for module, name in optional:
        test_import(module, name)
    
    print()
    print("=" * 60)
    
    if all_ok:
        print("✅ 所有核心依赖已正确安装！")
        print()
        print("下一步:")
        print("1. 运行: python main.py crawl --max-results 50")
        print("2. 运行: python main.py build-index")
        print("3. 运行: python main.py streamlit")
    else:
        print("❌ 部分依赖未安装")
        print()
        print("请运行: pip install -r requirements.txt")
        sys.exit(1)

if __name__ == "__main__":
    main()
